//
//  StarView.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/26.
//

#import "StarView.h"
#define imageW  self.bounds.size.width/10

@interface StarView ()

@property (nonatomic, strong) UIView *starBackgroundView;
@property (nonatomic, strong) UIView *starForegroundView;


@end


@implementation StarView
- (id)initWithFrame:(CGRect)frame EmptyImage:(NSString *)Empty StarImage:(NSString *)Star{
    
    
    
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.ischoose = YES;
        self.starBackgroundView = [self buidlStarViewWithImageName:Empty];
        self.starForegroundView = [self buidlStarViewWithImageName:Star];
        [self addSubview:self.starBackgroundView];
        
        self.userInteractionEnabled = YES;
        
        /**点击手势*/
        UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [self addGestureRecognizer:tapGR];
        
        /**滑动手势*/
        
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [self addGestureRecognizer:panGR];
        
        
        
    }
    return self;
    
}


- (UIView *)buidlStarViewWithImageName:(NSString *)imageName
{
    CGRect frame = self.bounds;
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.clipsToBounds = YES;
    
    
    for (int j = 0; j < 5; j ++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(2*j*imageW, 0, imageW, imageW);
        [view addSubview:imageView];
    }
    
    return view;
}

-(void)tapGR:(UITapGestureRecognizer *)tapGR{
    
    if (self.ischoose) {
        CGPoint point =[tapGR locationInView:self];
        if (point.x<0) {
            point.x = 0;
        }
        
        int X = (int) point.x/(2*imageW);
        NSInteger currentValue = X + 1;
        [_delegate starStarView:self currentValue:currentValue];
        self.starForegroundView.frame = CGRectMake(0, 0, (X+1)*2*imageW, imageW);
        [self addSubview:self.starForegroundView];
    }
}


// 重写set属性
- (void)setStarNumber:(NSInteger)starNumber{
    _starNumber = starNumber;
    self.starForegroundView.frame = CGRectMake(0, 0, (starNumber+1)*2*imageW, imageW);
    [self addSubview:self.starForegroundView];
    
}


- (void)setIschoose:(BOOL)ischoose{
    _ischoose = ischoose;
}




@end
